package com.ktwlsoft.archivemanage.domain.business.repositoryService.mend;

import com.alibaba.fastjson.JSONObject;
import com.ktwlsoft.archivemanage.domain.business.businessService.ImgUploadService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.ImgsService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Imgs;
import com.ktwlsoft.archivemanage.domain.dao.entity.mend.MendSubject;
import com.ktwlsoft.archivemanage.domain.dao.repository.mend.MendSubjectMapper;
import com.ktwlsoft.archivemanage.payload.businessInspect.ImgsUpdate;
import com.ktwlsoft.archivemanage.payload.mendList.MendSubjectImgModifyRequest;
import com.ktwlsoft.archivemanage.payload.mendList.MendSubjectModifyRequest;
import com.ktwlsoft.archivemanage.payload.mendList.MendSubjectOptionModifyRequest;
import com.ktwlsoft.archivemanage.payload.mendList.MendVerifySubject;
import com.ktwlsoft.archivemanage.payload.view.WeaknessTotalOfYear;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.Year;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MendSubjectRepositoryService {

    @Getter
    private final MendSubjectMapper mendSubjectMapper;

    @Getter
    private final ImgsService imgsService;

    public MendSubjectRepositoryService(MendSubjectMapper mendSubjectMapper, ImgsService imgsService) {
        this.mendSubjectMapper = mendSubjectMapper;
        this.imgsService = imgsService;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int updateMendSubject(List<MendSubjectModifyRequest> subjects) {
        int i = 0;
        for (MendSubjectModifyRequest request : subjects) {
            // 整改子项图片
            ImgUploadService.handleImg(request.getId(),"MendImg",request.getImgs() != null?
                    request.getImgs().stream().map(value ->{
                        ImgsUpdate imgsUpdate = new ImgsUpdate();
                        BeanUtils.copyProperties(value,imgsUpdate);
                        return imgsUpdate;
                    }).collect(Collectors.toList()):null);
            //选项图片
            for (MendSubjectOptionModifyRequest option : request.getOptions()) {
                ImgUploadService.handleImg(option.getOptionId(), "MendImg",option.getImgs() != null?
                        option.getImgs().stream().map(value ->{
                            ImgsUpdate imgsUpdate = new ImgsUpdate();
                            BeanUtils.copyProperties(value,imgsUpdate);
                            return imgsUpdate;
                        }).collect(Collectors.toList()):null);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",request.getId());
            jsonObject.put("mendRemark",request.getMendRemark());
            mendSubjectMapper.updateMendRemark(jsonObject);
            i++;
        }
        return i;
    }

    public int delete(Long id) {
        return mendSubjectMapper.deleteByPrimaryKey(id);
    }

    public List<MendSubject> findByMendId(Long mendId) {
        return mendSubjectMapper.selectByMendId(mendId);
    }

    public int save(MendSubject subject) {
        return mendSubjectMapper.insert(subject);
    }

    public int verify(MendVerifySubject subject) {
        return mendSubjectMapper.verify(subject);
    }


    public List<WeaknessTotalOfYear> findAllWeakness(Year year) {
        return mendSubjectMapper.findWeaknessTotalOfYear(year);
    }
}
